import random
from idlelib.editor import keynames
from threading import Thread

from playsound import playsound
from pynput import keyboard

# 记录按键按下次数，并播放音乐

# 统计按了多少下按键
count=0
# 可以播放的音乐列表
soundlist=['./sound/1.mp3','./sound/2.mp3','./sound/3.mp3']

# 按键回调
def Callback(key):
    print(key)
    global count
    count+=1
    if count%10 == 0:
        i=random.randint(0,len(soundlist)-1)
        # playsound(soundlist[i]) # 播放音频较为耗费时间，会导致程序卡顿
        # 创建线程执行播放动作
        t=Thread(target=playsound,args=(soundlist[i], ))
        t.start()

# 监听键盘状态，设置回调函数
listener=keyboard.Listener(on_release=Callback)
# 启动监听
listener.start()
# 等待监听结束
listener.join()

